<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="pdName">
        <el-input
          v-model="queryParams.pdName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="pdGoodType">
        <el-select
          v-model="queryParams.pdGoodType"
          placeholder="请选择物料类型"
          clearable
        >
          <el-option
            v-for="dict in getGbsGoodType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="pdGoodClass">
        <el-cascader
          v-model="queryParams.pdGoodClass"
          :options="allClasses"
          :props="classProps"
          :show-all-levels="false"
          style="width: 205px"
          placeholder="请选择物料分类"
        />
      </el-form-item>
      <el-form-item prop="pdGoodAttr">
        <el-select
          v-model="queryParams.pdGoodAttr"
          placeholder="请选择物料属性"
          clearable
        >
          <el-option
            v-for="dict in getGbsGoodAttr"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gbs:good:add']"
        >
          <span>新增</span>
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gbs:good:edit']"
        >修改
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gbs:good:remove']"
        >删除
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gbs:good:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编码" align="center" prop="pdCode" width="150" show-overflow-tooltip />
      <el-table-column label="物料名称" align="center" prop="pdName" width="120" show-overflow-tooltip />
      <el-table-column label="规格型号" align="center" prop="pdSpecType" width="180" show-overflow-tooltip />
      <el-table-column label="单位" align="center" prop="gbsGoodUnit.guName" width="80" show-overflow-tooltip />
      <el-table-column label="物料类型" align="center" prop="pdGoodType" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="getGbsGoodType" :value="scope.row.pdGoodType" />
        </template>
      </el-table-column>
      <el-table-column label="物料属性" align="center" prop="pdGoodAttr" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="getGbsGoodAttr" :value="scope.row.pdGoodAttr" />
        </template>
      </el-table-column>
      <el-table-column
        label="物料分类"
        align="center"
        prop="gbsGoodClass.gcName"
        show-overflow-tooltip
      />
      <el-table-column label="状态" align="center" prop="pdStatus" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="getStatus" :value="scope.row.pdStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            v-hasPermi="['gbs:good:query']"
            @click="handleDetail(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['gbs:good:edit']"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gbs:good:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { delGood, listGood } from '@/api/yyj/gbs/good'
import { allUnit } from '@/api/yyj/gbs/unit'
import { allClass } from '@/api/yyj/gbs/class'

export default {
  name: 'Material',
  dicts: ['gbs_good_type', 'gbs_good_attr', 'yyj_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品 通过 is_material 区分物料和产品表格数据
      goodList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isMaterial: 1,
        pdProuteCode: null,
        pdName: null,
        pdStatus: null,
        pdValidDate: null,
        pdAlarmDate: null,
        pdMax: null,
        pdMin: null,
        pdOriginPrice: null,
        pdSalePrice: null,
        pdDesc: null,
        pdSpecType: null,
        pdUnitType: null,
        gbsGoodUnit: null,
        pdGoodType: null,
        pdGoodAttr: null,
        pdGoodClass: null,
        gbsGoodClass: null
      },
      // 分类
      allClasses: [],
      classProps: {
        label: 'gcName',
        value: 'gcId',
        children: 'children',
        checkStrictly: true,
        emitPath: false
      },
      // 单位
      allUnits: []
    }
  },
  computed: {
    getGbsGoodType() {
      return this.dict.type.gbs_good_type
    },
    getGbsGoodAttr() {
      return this.dict.type.gbs_good_attr
    },
    getStatus() {
      return this.dict.type.yyj_common_status
    }
  },
  created() {
    Promise.all([this.getClasses(), this.getUnits(), this.getList()])
  },
  methods: {
    // 获取详情
    handleDetail(row) {
      this.$router.push({
        path: `/gbs/material-detail/${row.pdCode}`
      })
    },
    /** 查询物品 通过 is_material 区分物料和产品列表 */
    async getList() {
      this.loading = true
      const res = await listGood(this.queryParams)
      this.goodList = res.rows
      this.total = res.total
      this.loading = false
    },
    /** 查询分类 */
    async getClasses() {
      const res = await allClass({
        gcMaterial: '1'
      })
      this.allClasses = this.handleTree(res.data, 'gcId', 'gcFid')
    },
    /** 查询单位 */
    async getUnits() {
      const res = await allUnit()
      this.allUnits = res.data
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pdCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const pdCode = row.pdCode || this.ids
      this.$router.push({
        path: `/gbs/material-change/${pdCode}`
      })
    },
    // 新增
    handleAdd() {
      this.$router.push({
        path: `/gbs/material-change`
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pdCodes = row.pdCode || this.ids
      this.$modal
        .confirm('是否确认删除物料产品编号为"' + pdCodes + '"的数据项？')
        .then(function() {
          return delGood(pdCodes)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'yyj/gbs/good/export',
        {
          ...this.queryParams
        },
        `good_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
